<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试卷" prop="paperId">
        <el-input
          v-model="queryParams.paper"
          placeholder="请输入试卷"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.el_paper_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam:userPaper:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam:userPaper:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['exam:userPaper:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userPaperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户" align="center" prop="userName"/>
      <el-table-column label="试卷" align="center" prop="paper"/>
      <el-table-column label="得分" align="center" prop="userScore"/>
      <el-table-column label="所用时间" align="center" prop="userTime">
        <template slot-scope="scope">
          {{scope.row.totalTime-scope.row.userTime}}分钟
        </template>
      </el-table-column>
      <el-table-column label="总时长" align="center" prop="totalTime"/>
      <el-table-column label="总分" align="center" prop="totalScore"/>
      <el-table-column label="及格分" align="center" prop="qualifyScore"/>
      <el-table-column label="时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="状态" align="center" prop="status">

        <template slot-scope="scope">
<!--          <dict-tag :options="dict.type.el_user_paper_status" :value="scope.row.status"/>-->
          <el-tag v-if="scope.row.status == '0' " type="warning">未考</el-tag>
          <el-tag v-if="scope.row.status == '1' " type="danger">结束</el-tag>
          <el-tag v-if="scope.row.status == '2' " type="success">已考</el-tag>
          {{ scope.row.status }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="150px">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status!=='2'"
            type="text"
            size="mini"
            icon="el-icon-edit-outline"
            v-hasPermi="['exam:userPaper:query']">
            <router-link :to="'/exam/paper-start/index/' + scope.row.id"
                         class="link-type">
              {{scope.row.status==='0'?'开始':'继续'}}考试
            </router-link>
          </el-button>

          <el-button
            type="text"
            v-if="scope.row.status==='2'"
            size="mini"
            icon="el-icon-edit-outline"
            @click="showPaper(scope.row.questionList)"
            v-hasPermi="['exam:userPaper:query']"
          >试卷详情
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:userPaper:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  增加和修改弹窗  -->
    <dialog-edit v-if="openEdit" :visible.sync="openEdit" :modalObj='modalObj' @handleClose="handleClose"></dialog-edit>
    <paper v-if="openPaper" :visible.sync="openPaper" :questions='questions' @handleClose="handleClose"></paper>


  </div>
</template>

<script>
  import { listUserPaper, delUserPaper } from '@/api/exam/userPaper'
  import dialogEdit from './edit'
  import paper from './paper'

  export default {
    name: 'UserPaper',
    dicts: ['el_paper_status'],
    components: {
      dialogEdit,
      paper
    },
    data() {
      return {
        openPaper: false,
        modalObj: {},
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户试卷表格数据
        userPaperList: [],

        // 是否显示弹出层
        openEdit: false,
        // 查询参数
        queryParams: {
          orderByColumn: 'create_time',
          isAsc: 'desc',
          pageNum: 1,
          pageSize: 10,
          userName: null,
          paper: null,
          status: null
        },

        questions: []
      }

    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询用户试卷列表 */
      getList() {
        this.loading = true
        listUserPaper(this.queryParams).then(response => {
          this.userPaperList = response.rows
          this.total = response.total
          this.loading = false
        })
      },

      handleClose({ component, get }) {
        this[component] = false
        if (get) {
          this.getList()
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 显示试卷 */
      showPaper(data) {
        this.openPaper = true
        this.questions = JSON.parse(data)
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.openEdit = true //指定弹出框显示
        this.modalObj = { title: '添加', modal: true, width: '700px', id: '' }
      },

      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除用户试卷编号为"' + ids + '"的数据项？').then(function() {
          return delUserPaper(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },

      /** 导出按钮操作 */
      handleExport() {
        this.download('study/exam/userPaper/export', {
          ...this.queryParams
        }, `userPaper_${new Date().getTime()}.xlsx`)
      }

    }
  }
</script>
